package com.freight.order.serviceimpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freight.base.utils.AreaAddressUtil;
import com.freight.base.utils.AreaDtoUtil;
import com.freight.base.vo.UserVo;
import com.freight.core.common.constant.CommonConstant;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.utils.SecurityUtil;
import com.freight.core.entity.User;
import com.freight.core.service.SettingService;
import com.freight.order.dto.AreaPcDto;
import com.freight.order.dto.CompanyGoodsDto;
import com.freight.order.dto.CompanyVehicleDto;
import com.freight.order.dto.DriverGoodsDto;
import com.freight.order.mapper.OrderGoodsVoMapper;
import com.freight.order.mapper.OrderMainMapper;
import com.freight.order.service.IOrderMainMapperService;
import com.freight.order.vo.*;
import com.freight.user.service.ICompanyVoService;
import com.freight.user.service.IDriverVoService;
import com.freight.user.util.UserVoUtils;
import com.freight.user.vo.AppointCompanyInfoVo;
import com.freight.user.vo.AppointDriverInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 司机空车需求详情接口实现
 *
 * @author huyingtai
 */
@Slf4j
@Service
@Transactional
public class IOrderMainMapperServiceImpl extends ServiceImpl<OrderMainMapper, UserVo>
        implements IOrderMainMapperService {

    @Autowired
    private OrderMainMapper orderMainMapper;

    @Autowired
    private OrderGoodsVoMapper orderGoodsVoMapper;

    @Autowired
    private AreaAddressUtil areaAddressUtil;

    @Autowired
    private IDriverVoService iDriverVoService;

    @Autowired
    private ICompanyVoService iCompanyVoService;



    @Autowired
    private SettingService settingService;



    @Autowired
    SecurityUtil securityUtil;

    /**
     * 司机端 --- 获取已报价货源
     * @param page
     * @param driverGoodsDto
     * @return
     */
    @Override
    public Page<DriverGoodsVo> getQuotedorderGoods(Page<DriverGoodsVo> page, DriverGoodsDto driverGoodsDto) {

        User u = securityUtil.getCurrUser();
        if (u == null) {
            throw new FreightException("登入失效或尚未登入，请先登入");
        }

        driverGoodsDto = AreaDtoUtil.getAreaDtoSeatch(driverGoodsDto);
        Page<DriverGoodsVo> res = orderMainMapper.getQuotedorderGoods(page, driverGoodsDto, u.getId());
        List<DriverGoodsVo> records = res.getRecords();
        areaAddressUtil.fillAreaAddress(records);
        HashSet<Long> companyUserIds = new HashSet<>();
        for (DriverGoodsVo record : records) {
            companyUserIds.add(record.getCompanyUserId());
        }
        Long[] comUserIds = companyUserIds.stream().toArray(Long[]::new);
        List<Long> comUserIdList = Arrays.asList(comUserIds);

        // 获取货主信息
        List<AppointCompanyInfoVo> aCompanys = iCompanyVoService.getAppointCompanyInfoVoByUserIds(comUserIds);

        HashMap<Long, AppointCompanyInfoVo> aMapCompany = UserVoUtils.listToHashMap(aCompanys);

        for (DriverGoodsVo record : records) {
            AppointCompanyInfoVo company = aMapCompany.get(record.getCompanyUserId());
            record.setCargoOwnerInfoVo(company);
        }
        return res;
    }


    @Override
    public PlatformGoodsVo getGoodsDetails(Long goodsid) {
        User u = securityUtil.getCurrUser();
        if (u == null) {
            throw new FreightException("登入失效或尚未登入，请先登入");
        }

        PlatformGoodsVo res = orderMainMapper.getGoodsDetails(goodsid);
        areaAddressUtil.fillAreaAddress(res);
        HashSet<Long> companyUserIds = new HashSet<>();
        companyUserIds.add(res.getCompanyUserId());


        Long[] comUserIds = companyUserIds.stream().toArray(Long[]::new);
        List<Long> comUserIdList = Arrays.asList(comUserIds);

        // 获取货主信息
        List<AppointCompanyInfoVo> aCompanys = iCompanyVoService.getAppointCompanyInfoVoByUserIds(comUserIds);
        HashMap<Long, AppointCompanyInfoVo> aMapCompany = UserVoUtils.listToHashMap(aCompanys);

        AppointCompanyInfoVo company = aMapCompany.get(res.getCompanyUserId());
        res.setCargoOwnerInfoVo(company);

        List<String> loadingList = orderGoodsVoMapper.getLoadingTime(res.getGoodId(), null);
        List<String> loadingNotList = orderGoodsVoMapper.getLoadingTime(res.getGoodId(), "1");

        res.setLoadingNotTimeList(loadingNotList);
        res.setLoadingTimeList(loadingList);
        return res;

    }

    /**
     * 货主配货大厅 返回自己的货源信息
     *
     * @param page
     * @param companyGoodsDto
     * @return
     */
    @Override
    public Page<CompanyGoodsVo> getCompanyGoodsInfo(Page<CompanyGoodsVo> page, CompanyGoodsDto companyGoodsDto) {
        User u = securityUtil.getCurrUser();
        if (u == null) {
            throw new FreightException("登入失效或尚未登入，请先登入");
        }
        companyGoodsDto.setUserId(u.getId());
        // 当前用户是 货主 只能看到自己的货物 否则可以看到条件搜索的
//        if (u.getUserType().equals(CommonConstant.USER_TYPE_COMPANY)) {
//            companyGoodsDto.setUserId(u.getId());
//        }
        // 拼凑地区搜索条件
        companyGoodsDto = AreaDtoUtil.getAreaDtoSeatch(companyGoodsDto);
        Page<CompanyGoodsVo> res = orderMainMapper.getCompanyGoodsVo(page, companyGoodsDto);

        List<CompanyGoodsVo> records = res.getRecords();
        areaAddressUtil.fillAreaAddress(records);
        HashSet<Long> companyUserIds = new HashSet<>();

        for (CompanyGoodsVo companyGoodsVo: records ) {
            companyUserIds.add(companyGoodsVo.getCompanyUserId());

        }

        Long[] comUserIds = companyUserIds.stream().toArray(Long[]::new);
        List<Long> comUserIdList = Arrays.asList(comUserIds);

        // 获取货主信息
        List<AppointCompanyInfoVo> aCompanys = iCompanyVoService.getAppointCompanyInfoVoByUserIds(comUserIds);

        HashMap<Long, AppointCompanyInfoVo> aMapCompany = UserVoUtils.listToHashMap(aCompanys);

        for (CompanyGoodsVo record : records) {
            AppointCompanyInfoVo company = aMapCompany.get(record.getCompanyUserId());
            record.setCargoOwnerInfoVo(company);
        }

        return res;
    }

    @Override
    public Page<CompanyGoodsVo> quotationTrack(Page<CompanyGoodsVo> page, CompanyGoodsDto companyGoodsDto) {

        User u = securityUtil.getCurrUser();
        if (u == null) {
            throw new FreightException("登入失效或尚未登入，请先登入");
        }
        // 当前用户是 货主 只能看到自己的货物 否则可以看到条件搜索的
        if (u.getUserType().equals(CommonConstant.USER_TYPE_COMPANY)) {
            companyGoodsDto.setUserId(u.getId());
        }
        // 拼凑地区搜索条件
        companyGoodsDto = AreaDtoUtil.getAreaDtoSeatch(companyGoodsDto);
        companyGoodsDto.setUserId(u.getId());
        Page<CompanyGoodsVo> res = orderMainMapper.quotationTrack(page, companyGoodsDto);

        List<CompanyGoodsVo> records = res.getRecords();
        areaAddressUtil.fillAreaAddress(records);
        return res;
    }

    /**
     * 司机货源首页
     *
     * @param page
     * @param driverGoodsDto
     * @return
     */
    @Override
    public Page<DriverGoodsVo> getDriverGoodsInfo(Page<DriverGoodsVo> page, DriverGoodsDto driverGoodsDto) {
        User u = securityUtil.getCurrUser();
        if (u == null) {
            throw new FreightException("登入失效或尚未登入，请先登入");
        }

        driverGoodsDto = AreaDtoUtil.getAreaDtoSeatch(driverGoodsDto);

        Long[] companys = null;

        Page<DriverGoodsVo> res = orderMainMapper.getDriverGoodsVo(page, driverGoodsDto, companys);
        List<DriverGoodsVo> records = res.getRecords();
        areaAddressUtil.fillAreaAddress(records);
        HashSet<Long> companyUserIds = new HashSet<>();
        for (DriverGoodsVo record : records) {
            companyUserIds.add(record.getCompanyUserId());
        }
        Long[] comUserIds = companyUserIds.stream().toArray(Long[]::new);

        // 获取货主信息
        List<AppointCompanyInfoVo> aCompanys = iCompanyVoService.getAppointCompanyInfoVoByUserIds(comUserIds);
        HashMap<Long, AppointCompanyInfoVo> aMapCompany = UserVoUtils.listToHashMap(aCompanys);

        for (DriverGoodsVo record : records) {
            AppointCompanyInfoVo company = aMapCompany.get(record.getCompanyUserId());
            record.setCargoOwnerInfoVo(company);
        }
        return res;
    }

    /**
     * 司机货源首页
     *
     * @param page
     * @return
     */
    @Override
    public Page<PlatformGoodsVo> getPcDriverGoodInfoByPage(Page<DriverGoodsVo> page, AreaPcDto areaPcDto) {
        User u = securityUtil.getCurrUser();
        if (u == null) {
            throw new FreightException("登入失效或尚未登入，请先登入");
        }

        areaPcDto = AreaDtoUtil.getAreaDtoSeatch(areaPcDto);

        Long[] companys = null;

        Page<PlatformGoodsVo> res = orderMainMapper.getPcDriverGoodInfoByPage(page, areaPcDto, companys);
        List<PlatformGoodsVo> records = res.getRecords();
        areaAddressUtil.fillAreaAddress(records);
        HashSet<Long> companyUserIds = new HashSet<>();
        for (PlatformGoodsVo record : records) {
            companyUserIds.add(record.getCompanyUserId());
        }
        Long[] comUserIds = companyUserIds.stream().toArray(Long[]::new);
        List<Long> comUserIdList = Arrays.asList(comUserIds);

        // 获取货主信息
        List<AppointCompanyInfoVo> aCompanys = iCompanyVoService.getAppointCompanyInfoVoByUserIds(comUserIds);
        HashMap<Long, AppointCompanyInfoVo> aMapCompany = UserVoUtils.listToHashMap(aCompanys);

        for (PlatformGoodsVo record : records) {
            AppointCompanyInfoVo company = aMapCompany.get(record.getCompanyUserId());
            record.setCargoOwnerInfoVo(company);
        }
        return res;
    }
}